/**
 * 
 */
package com.qunar.crawler.worker;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qunar.crawler.DB.DBUtil;
import com.qunar.crawler.bean.Go117;
import com.qunar.crawler.html.HtmlAction;
import com.qunar.crawler.html.HtmlPossesser117Go;
import com.qunar.crawler.utils.UrlFrontier;

/**
 * @author tao.zhang
 */
public class Go117Crawler extends Thread {

	private int index;
	
	private HtmlAction http;
	
	private static final Logger logger = LoggerFactory
			.getLogger(Go117Crawler.class);


	public Go117Crawler(int index) {
		this.index = index;
	}

	@Override
	public String toString() {
		return "Go117Crawler [toString()=" + this.index + "]";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Thread#run()
	 */
	public void run() {

		http = new HtmlAction();
		// get the day of thedaybeforeyesteday
		Calendar c = Calendar.getInstance();
		SimpleDateFormat fmt = new SimpleDateFormat(
				"yyyy/MM/dd");
		c.add(Calendar.DATE, -5);
		String theDayBeforeYesteday = fmt.format(c.getTime());
		System.out.println(theDayBeforeYesteday);

		while (true) {
			try {
				Thread.sleep(RandomUtils.nextInt(10) * 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			// if the queue is empty the end the work;
			if (UrlFrontier.getInstance().isEmpty()){
				logger.info("队列中没有URl");
				break;
			}
			String url = null;
			url = UrlFrontier.getInstance().take();
			// get next url
			if (StringUtils.isBlank(url))
				continue;

			// loop send request until success
			String page = null;
			while (StringUtils.isBlank(page)) {
				page = http.get(url);
			}
			List<Go117> go117s = new LinkedList<Go117>();
			
			logger.info("抽取图片…………");
			HtmlPossesser117Go.digGo117(page, go117s);

			for(Go117 go:go117s){
				System.out.println(go);
			}
			DBUtil.addGo1172DB(go117s);

			// collection not empty and the last element's date <=
			// thedaybeforeyesteday .....then break the loop
			if (CollectionUtils.isNotEmpty(go117s)
					&& go117s.get(go117s.size() - 1).getDate()
							.compareTo(theDayBeforeYesteday) == 0){
					System.out.println("已经抓取到前天的图片，结束抓取");
				break;
			}

		}

		logger.info(this.toString()
				+ "=====>>>爬虫结束>>"
				+ new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss")
						.format(new Date()));

	}
}
